import {createRouter, createWebHashHistory} from 'vue-router'
import {reactive} from "vue";
import {ElMessage} from "element-plus";
import {getSessionStr, setLocalStr} from "@/utils/common.ts";

let options = reactive({
    routes: [
        {
            path: "/",
            redirect: "/homepage"
        },
        {
            path: "/homepage",
            component: () => import(`@/views/Homepage.vue`),
        },
        {
            path: "/data/list",
            component: () => import(`@/views/data/DataList.vue`),
        },
        {
            path: "/data/content",
            component: () => import(`@/views/data/DataContent.vue`),
        },
    ],
    history: createWebHashHistory()
})
const router = createRouter(options);
router.beforeEach((to, from, next) => {
    if (to.path === '/center') {
        if (getSessionStr('token') === null) {
            ElMessage.warning('请先登录');
            setLocalStr('activeMenu', '/login')
            next('/login');
        } else {
            setLocalStr('activeMenu', '/center')
            next();
        }
    } else if (to.path === '/login') {
        if (getSessionStr('token') !== null) {
            ElMessage.success('已登录,自动跳转到个人中心');
            setLocalStr('activeMenu', '/center')
            next('/center');
        } else {
            setLocalStr('activeMenu', '/login')
            next();
        }
    } else {
        setLocalStr('activeMenu', to.path)
        next();
    }
})
export default router
